package com.ihighest.app.infox.activity;

import java.util.HashMap;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.ReportReasonAdapter;

/**
 * 内容举报页
 * 
 * @author lkx Created at 2015-10-16 下午9:05:35
 * @version 1.0
 */
public class ReportActivity extends BaseActivity {
	private TextView commit;
	private ListView reasonList;
	private EditText descriptionView;
	public static String reason;
	private ReportReasonAdapter adapter;
	private int prePosition = 0;
	HashMap<Integer, Boolean> isSelect;
	private String chooseItem;
	private String newsId;// 新闻id
	private String newType;// 新闻类型

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_report);
		newsId = getIntent().getStringExtra("newsId");
		newType = getIntent().getStringExtra("newType");
		initView();
		initData();
	}

	/**
	 * 初始化数据
	 * 
	 * 
	 * @author lkx Created at 2015-10-19 下午7:26:37
	 */
	private void initData() {
		Api.data("inform_cause_list.json", new ApiCallback(ReportActivity.this) {

			@Override
			public void handleSuccess(ApiResult result) {
				if (result.getDatas() != null || result.getDatas().length() != 0) {
					// for (int i = 0; i < result.getDatas().length(); i++) {
					// isSelect.put(i, false);
					// }
					adapter = new ReportReasonAdapter(ReportActivity.this, result.getDatas(), isSelect);
					reasonList.setAdapter(adapter);
				}

			}

			@Override
			public void handleFailure(Exception e, String msg) {
				// TODO Auto-generated method stub

			}
		}, ReportActivity.this);
	}

	/**
	 * 绑定控件
	 * 
	 * 
	 * @author lkx Created at 2015-10-19 下午5:43:02
	 */
	private void initView() {
		commit = (TextView) findViewById(R.id.reportCommit);
		reasonList = (ListView) findViewById(R.id.reportContentListview);
		descriptionView = (EditText) findViewById(R.id.descriptionView);
		if (isSelect != null)
			isSelect = null;
		isSelect = new HashMap<Integer, Boolean>();

		findViewById(R.id.back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();

			}
		});
		reasonList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// CheckBox cb = (CheckBox)
				// arg1.findViewById(R.id.chooseReport);
				// cb.setChecked(true);
				// if (cb.isChecked()) {
				// chooseItem = cb.getText().toString();
				// }
				// View v = reasonList.getChildAt(prePosition);
				// CheckBox preCb = (CheckBox)
				// v.findViewById(R.id.chooseReport);
				// preCb.setChecked(false);
				// prePosition = arg2;

			}
		});
		commit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				ApiParam param = ApiParam.create();
				param.addParam("id", newsId);
				param.addParam("type", newType);
				if (!TextUtils.isEmpty(reason)) {
					param.addParam("cause", reason);
					if (!TextUtils.isEmpty(descriptionView.getText().toString().trim())) {
						param.addParam("memo", descriptionView.getText().toString().trim());
					}

					Api.post("inform", param, new ApiCallback(ReportActivity.this) {

						@Override
						public void handleSuccess(ApiResult result) {
							finish();
						}

						@Override
						public void handleFailure(Exception e, String msg) {
							// TODO Auto-generated method stub

						}
					}, ReportActivity.this);
				} else {
					Toast.makeText(ReportActivity.this, "举报原因不能为空", 0).show();
				}

			}
		});
	}
}
